package com.group18.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.group18.entity.FamilyUser;
import com.group18.entity.MemberUser;
import com.group18.service.FamilyUserService;
import com.group18.service.MemberUserService;


/**
 * Servlet implementation class DoRegister
 */
@WebServlet("/DoRegister")
public class DoRegister extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoRegister() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		
		String msg = "";
		String username = request.getParameter("username_register");
		String password = request.getParameter("password_register");
		String family = request.getParameter("family");
		
		//添加用户
		FamilyUser fu = new FamilyUser();
		fu.setName(family);
		FamilyUserService fus = new FamilyUserService();
		int family_id = fus.selectIDByName(fu);
		
		MemberUser mu = new MemberUser();
		mu.setName(family);
		mu.setFamily_id(family_id);
		mu.setUsername(username);
		mu.setPassword(password);
		MemberUserService mus = new MemberUserService();
		
		boolean bl = mus.isExist(mu);
		if(bl) {
			msg = "该用户已存在";
			request.setAttribute("msg", msg);
		}else {
			int i = mus.addUser(mu);
			if(i == 1) {
				msg = "注册成功";
				request.setAttribute("msg", msg);
			}else {
				msg = "注册失败";
				request.setAttribute("msg", msg);
			}
		}
		
		String jump = request.getParameter("jump");
		request.getRequestDispatcher(jump).forward(request, response);

	}

}
